package gnss

/*
*

	存储一个历元的Buffer
	只接收观测值的Buffer
*/
type GnssRtcmEpoch struct {
	sow         int // 周内秒
	current     []*GnssRtcmMsmBufRec
	OnEpochFunc func(sow int, lst []*GnssRtcmMsmBufRec)
}

func NewGnssRtcmEpoch() *GnssRtcmEpoch {
	rval := &GnssRtcmEpoch{}
	return rval
}

/*
*

	包含了头和CRC
	-1: 不是msm数据包
*/
func (this *GnssRtcmEpoch) InputRtcmBuf(rtcmRawBuf []byte) int {
	rec := NewGnssRtcmMsmBufRec()
	rec.MsgID, rec.StaId, rec.NaviSys, rec.Sow, rec.Sync = ExtractInfoFromRtcmBuf(rtcmRawBuf, 24)
	if rec.Sow == 0 {
		return -1
	}
	rec.Raw = rtcmRawBuf
	if this.sow == 0 {
		this.current = make([]*GnssRtcmMsmBufRec, 0, 16)
	} else if this.sow != rec.Sow && rec.NaviSys != SYS_GLO { // GLO 跳秒18秒是固定的? 是否会递增? 不确定性? 暂时排除
		curr := this.current
		if this.OnEpochFunc != nil {
			this.OnEpochFunc(this.sow, curr)
		}
		this.current = make([]*GnssRtcmMsmBufRec, 0, 16)
		this.sow = rec.Sow
	}
	this.sow = rec.Sow
	this.current = append(this.current, rec)
	if rec.Sync == 0 {
		if this.OnEpochFunc != nil {
			this.OnEpochFunc(this.sow, this.current)
		}
		this.sow = 0
	}
	return 0
}
